home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / libs / stk110 / demosrc.com / SMSPR.C < prev    next >
C/C++ Source or Header  |  1991-02-25  |  12KB  |  310 lines

  1. /**********************************************************************
  2. * smspr.c
  3. *
  4. * StarMines - a sprite toolkit demonstration game.
  5. *
  6. * The sprite creation functions.
  7. **********************************************************************
  8.                     This file is part of
  9.  
  10.           STK -- The sprite toolkit -- version 1.1
  11.  
  12.               Copyright (C) Jari Karjala 1991
  13.  
  14. The sprite toolkit (STK) is a FreeWare toolkit for creating high
  15. resolution sprite graphics with PCompatible hardware. This toolkit 
  16. is provided as is without any warranty or such thing. See the file
  17. COPYING for further information.
  18.  
  19. **********************************************************************/
  20.  
  21. #include "stk.h"
  22.  
  23. #include "sm.h"
  24. #include "smspr.h"
  25.  
  26. #include "smp\ship1.smp"
  27. #include "smp\ship2.smp"
  28. #include "smp\ship3.smp"
  29. #include "smp\ship4.smp"
  30. #include "smp\ship5.smp"
  31. #include "smp\ship6.smp"
  32. #include "smp\ship7.smp"
  33. #include "smp\ship8.smp"
  34. #include "smp\ship9.smp"
  35. #include "smp\ship10.smp"
  36. #include "smp\ship11.smp"
  37. #include "smp\ship12.smp"
  38. #include "smp\ship13.smp"
  39. #include "smp\ship14.smp"
  40. #include "smp\ship15.smp"
  41. #include "smp\ship16.smp"
  42.  
  43. #include "smp\bullet.smp"
  44.  
  45. #include "smp\expl1.smp"
  46. #include "smp\expl2.smp"
  47. #include "smp\expl3.smp"
  48. #include "smp\expl4.smp"
  49.  
  50. #include "smp\mine1a.smp"
  51. #include "smp\mine1b.smp"
  52. #include "smp\mine2a.smp"
  53. #include "smp\mine2b.smp"
  54. #include "smp\mine3a.smp"
  55. #include "smp\mine3b.smp"
  56. #include "smp\mine4a.smp"
  57. #include "smp\mine4b.smp"
  58.  
  59. typedef struct mine_bitmap {
  60.     BITMAP mine1, mask1, mine2, mask2;
  61.     BYTE w,h;
  62. } MINE_BITMAP;
  63.  
  64. #define MAX_MINES 4
  65. MINE_BITMAP mines[MAX_MINES] = {
  66.     {   mine1a_shape, mine1a_mask, mine1b_shape, mine1b_mask, 
  67.         mine1a_width, mine1a_height },
  68.  
  69.     {   mine2a_shape, mine2a_mask, mine2b_shape, mine2b_mask, 
  70.         mine2a_width, mine2a_height },
  71.  
  72.     {   mine3a_shape, mine3a_mask, mine3b_shape, mine3b_mask, 
  73.         mine3a_width, mine3a_height },
  74.  
  75.     {   mine4a_shape, mine4a_mask, mine4b_shape, mine4b_mask, 
  76.         mine4a_width, mine4a_height },
  77. };
  78.  
  79. /**********************************************************************
  80. * Create the wave'th aliens
  81. * Return: 0 if OK, negative if out of memory, etc
  82. **********************************************************************/
  83. int smspr_init_aliens(int wave) 
  84. {
  85.     int ind;
  86.  
  87.     /** kill all existing aliens **/
  88.     for (ind=0; ind<MAX_ALIEN; ind++) {
  89.         if (aliens[ind].as!=NULL)
  90.             spr_anim_delete(aliens[ind].as);
  91.         if (aliens[ind].s1!=NULL)
  92.             spr_delete(aliens[ind].s1);
  93.         if (aliens[ind].s2!=NULL)
  94.             spr_delete(aliens[ind].s2);
  95.         aliens[ind].active = 0;
  96.     }
  97.     /** do one pass to really get rid of the sprites **/
  98.     spr_anim_next_pass();
  99.     
  100.     wave %= MAX_MINES;
  101.     
  102.     /** create new ones **/
  103.     aliens[0].s1 = spr_create(mines[wave].w, mines[wave].h, 
  104.                               mines[wave].mine1, mines[wave].mask1, 
  105.                               sprite_resolution, 0);
  106.     aliens[0].s2 = spr_create(mines[wave].w, mines[wave].h, 
  107.                               mines[wave].mine2, mines[wave].mask2, 
  108.                               sprite_resolution, 0);
  109.     aliens[0].s1 = spr_share(aliens[0].s1, MAX_ALIEN);
  110.     aliens[0].s2 = spr_share(aliens[0].s2, MAX_ALIEN);
  111.  
  112.     if (aliens[0].s1==NULL)
  113.         return -1;
  114.     if (aliens[0].s2==NULL)
  115.         return -1;
  116.  
  117.     aliens[0].as = spr_anim_create(2,aliens[0].s1,aliens[0].s2);
  118.     if (aliens[0].as==NULL)
  119.         return -1;
  120.     
  121.  
  122.     for (ind=1; ind<MAX_ALIEN; ind++) {
  123.         aliens[ind].s1 = spr_copy(aliens[0].s1, ind);
  124.         aliens[ind].s2 = spr_copy(aliens[0].s2, ind);
  125.         aliens[ind].as = spr_anim_create(2,aliens[ind].s1,aliens[ind].s2);
  126.     }
  127.     
  128.     return 0;
  129. }
  130.     
  131.  
  132.  
  133. /**********************************************************************
  134. * Create an animated alien into the next free slot in the global 
  135. * 'aliens' array. Set fx_handler and start alien.
  136. * Return NULL if error or no more slots.
  137. **********************************************************************/
  138. ANIM_SPRITE smspr_create_alien(void)
  139. {
  140.     int ind;
  141.  
  142.     for(ind=0; ind<MAX_ALIEN && aliens[ind].active; ind++)
  143.         ;
  144.     if (ind==MAX_ALIEN)
  145.         return NULL;
  146.  
  147.     if (aliens[ind].as==NULL)
  148.         aliens[ind].as = spr_anim_create(2,aliens[ind].s1,aliens[ind].s2);
  149.     
  150.     if (aliens[ind].as==NULL)
  151.         return NULL;
  152.     
  153.     /** we do not need hit detection for aliens **/
  154.     spr_anim_set_fx_handler(aliens[ind].as, 
  155.                             SPR_ANIM_FX_ALL & ~SPR_ANIM_FX_HIT_SPRITE,
  156.                             alien_fx_handler);
  157.     spr_anim_start(aliens[ind].as);
  158.     aliens[ind].active = 1;
  159.     aliens[ind].divide = 0;
  160.     aliens[ind].divided = 0;
  161.     aliens[ind].shot = 0;
  162.     
  163.     return aliens[ind].as;
  164. }
  165.  
  166. /**********************************************************************
  167. * Creates an 'animated' sprite for the player. Actually it contains
  168. * only the different rotations for the player. Set fx_handler and 
  169. * start the player.
  170. * Return: NULL if error, the player otherwise.
  171. **********************************************************************/
  172. ANIM_SPRITE smspr_create_player(void)
  173. {
  174.     ANIM_SPRITE as;
  175.     
  176.     
  177.     as = spr_anim_create(16,
  178.                          spr_create(ship1_width, ship1_height, 
  179.                                     ship1_shape, ship1_mask, 
  180.                                     sprite_resolution, PLAYER_ID),
  181.                          spr_create(ship2_width, ship2_height, 
  182.                                     ship2_shape, ship2_mask, 
  183.                                     sprite_resolution, PLAYER_ID),
  184.                          spr_create(ship3_width, ship3_height, 
  185.                                     ship3_shape, ship3_mask, 
  186.                                     sprite_resolution, PLAYER_ID),
  187.                          spr_create(ship4_width, ship4_height, 
  188.                                     ship4_shape, ship4_mask, 
  189.                                     sprite_resolution, PLAYER_ID),
  190.                          spr_create(ship5_width, ship5_height, 
  191.                                     ship5_shape, ship5_mask, 
  192.                                     sprite_resolution, PLAYER_ID),
  193.                          spr_create(ship6_width, ship6_height, 
  194.                                     ship6_shape, ship6_mask, 
  195.                                     sprite_resolution, PLAYER_ID),
  196.                          spr_create(ship7_width, ship7_height, 
  197.                                     ship7_shape, ship7_mask, 
  198.                                     sprite_resolution, PLAYER_ID),
  199.                          spr_create(ship8_width, ship8_height, 
  200.                                     ship8_shape, ship8_mask, 
  201.                                     sprite_resolution, PLAYER_ID),
  202.                          spr_create(ship9_width, ship9_height, 
  203.                                     ship9_shape, ship9_mask, 
  204.                                     sprite_resolution, PLAYER_ID),
  205.                          spr_create(ship10_width, ship10_height, 
  206.                                     ship10_shape, ship10_mask, 
  207.                                     sprite_resolution, PLAYER_ID),
  208.                          spr_create(ship11_width, ship11_height, 
  209.                                     ship11_shape, ship11_mask, 
  210.                                     sprite_resolution, PLAYER_ID),
  211.                          spr_create(ship12_width, ship12_height, 
  212.                                     ship12_shape, ship12_mask, 
  213.                                     sprite_resolution, PLAYER_ID),
  214.                          spr_create(ship13_width, ship13_height, 
  215.                                     ship13_shape, ship13_mask, 
  216.                                     sprite_resolution, PLAYER_ID),
  217.                          spr_create(ship14_width, ship14_height, 
  218.                                     ship14_shape, ship14_mask, 
  219.                                     sprite_resolution, PLAYER_ID),
  220.                          spr_create(ship15_width, ship15_height, 
  221.                                     ship15_shape, ship15_mask, 
  222.                                     sprite_resolution, PLAYER_ID),
  223.                          spr_create(ship16_width, ship16_height, 
  224.                                     ship16_shape, ship16_mask, 
  225.                                     sprite_resolution, PLAYER_ID));
  226.    if (as==NULL)
  227.        return NULL;
  228.    
  229.    spr_anim_set_fx_handler(as, SPR_ANIM_FX_ALL, player_fx_handler);
  230.    spr_anim_start(as);
  231.    return as; 
  232. }
  233.  
  234. /**********************************************************************
  235. * Create a bullet. Set fx_handler and start the bullet.
  236. * Return: NULL if error, the bullet otherwise.
  237. **********************************************************************/
  238. ANIM_SPRITE smspr_create_bullet(void)
  239. {
  240.     static SPRITE ss = NULL;
  241.     ANIM_SPRITE as;
  242.         
  243.     if (ss==NULL) {
  244.         ss = spr_create(bullet_width, bullet_height, 
  245.                             bullet_shape, bullet_mask, 
  246.                             sprite_resolution, BULLET_ID);
  247.         ss = spr_share(ss, 50); /** max 50 bullets **/
  248.     }
  249.     as = spr_anim_create(1, spr_copy(ss, BULLET_ID));
  250.                              
  251.     if (as==NULL)
  252.         return NULL;
  253.    
  254.     spr_anim_set_fx_handler(as, SPR_ANIM_FX_ALL, bullet_fx_handler);
  255.     spr_anim_start(as);
  256.     return as;     
  257. }
  258.  
  259.  
  260. /**********************************************************************
  261. * Create an explosion. Set fx_handler and start the sprite.
  262. * Return: NULL if error, the ANIM_SPRITE otherwise.
  263. **********************************************************************/
  264. ANIM_SPRITE smspr_create_explosion(void)
  265. {
  266.     static SPRITE ss1 = NULL, ss2 = NULL, ss3 = NULL, ss4 = NULL;
  267.     ANIM_SPRITE as;
  268.     int ind;
  269.     
  270.     if (ss1==NULL) {
  271.         ss1 = spr_create(expl1_width, expl1_height, expl1_shape, expl1_mask, 
  272.                          4, EXPLO_ID);
  273.         ss2 = spr_create(expl2_width, expl2_height, expl2_shape, expl2_mask, 
  274.                          4, EXPLO_ID);
  275.         ss3 = spr_create(expl3_width, expl3_height, expl3_shape, expl3_mask, 
  276.                          4, EXPLO_ID);
  277.         ss4 = spr_create(expl4_width, expl4_height, expl4_shape, expl4_mask, 
  278.                          4, EXPLO_ID);
  279.         ss1 = spr_share(ss1, 20);
  280.         ss2 = spr_share(ss2, 20);
  281.         ss3 = spr_share(ss3, 20);
  282.         ss4 = spr_share(ss4, 20);
  283.         for (ind=0; ind<MAX_EXPL; ind++) {
  284.             expls[ind].active = 0;
  285.             expls[ind].as = spr_anim_create(4, 
  286.                                           spr_copy(ss1,EXPLO_ID+ind), 
  287.                                           spr_copy(ss2,EXPLO_ID+ind), 
  288.                                           spr_copy(ss3,EXPLO_ID+ind), 
  289.                                           spr_copy(ss4,EXPLO_ID+ind));
  290.             if (expls[ind].as==NULL)
  291.                 break;
  292.         }
  293.     }
  294.  
  295.     for(ind=0; ind<MAX_EXPL && expls[ind].active; ind++)
  296.         ;
  297.     if (ind==MAX_EXPL)
  298.         return NULL;
  299.  
  300.     if (expls[ind].as==NULL)
  301.         return NULL;
  302.     
  303.     spr_anim_set_fx_handler(expls[ind].as, 
  304.                             SPR_ANIM_FX_ALL & ~SPR_ANIM_FX_HIT_SPRITE,
  305.                             explo_fx_handler);
  306.     spr_anim_start(expls[ind].as);
  307.     expls[ind].active = 1;
  308.     return expls[ind].as;     
  309. }
  310.